package com.kefu.chat.service;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.kefu.chat.mapper.ChatReplyMapper;
import com.kefu.chat.model.ChatReply;
import com.kefu.chat.vo.ChatReplyInsertVo;
import com.kefu.common.context.KefuContext;
import com.kefu.common.service.AbstractKefuService;

import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;

import javax.annotation.Resource;

@Service
@Transactional
public class ChatReplyService  extends AbstractKefuService<ChatReply>  {
    @Resource
    protected ChatReplyMapper chatReplyMapper;

	public int getMaxWeight(String tenantId, String parentId){
		return chatReplyMapper.getMaxWeight(tenantId,parentId);
	}

	 
	public void deleteByParentId(String tenantId, String parentId) {
		chatReplyMapper.deleteByParentId(tenantId,parentId);
	}

	 
	public String importReply(InputStream inputStream) {
		ExcelReader excelReader = ExcelUtil.getReader(inputStream);
		List<Map<String, Object>> list = excelReader.read(0, 1, 1000);
		String tenantId = KefuContext.getKefuContext().getTenantId();

		//查询快捷回复分类，回复和分类在同一张表，分类就是ParentId为0的数据
		List<ChatReply> replyParentList = list(Wrappers.lambdaQuery(ChatReply.class).eq(ChatReply::getTenantId,tenantId).eq(ChatReply::getParentId,"0"));
		for (int i =0; i< list.size(); i++) {
			try {
				Map<String, Object> map = list.get(i);
				String parentName = String.valueOf(map.get("快捷回复分类"));
				String replyContent = String.valueOf(map.get("快捷回复内容"));
				String replayType = String.valueOf(map.get("快捷回复类型"));
				if (StrUtil.isBlankOrUndefined(parentName)
						|| StrUtil.isBlankOrUndefined(replyContent)) {
					logger.info("导入错误:{},{},{}", i + 1, parentName, replyContent);
					continue;
				}
				ChatReplyInsertVo insertVo = new ChatReplyInsertVo();
				insertVo.setName(replyContent.replaceAll("\r|\n", "").trim());
				if (!StrUtil.isBlankOrUndefined(replayType) && "公共".equals(replayType)) {
					insertVo.setType(0);
				}else {
					insertVo.setType(1);
				}
				for (ChatReply reply: replyParentList) {
					if (parentName.equals(reply.getName()) && Objects.equals(insertVo.getType(), reply.getType())) {
						insertVo.setParentId(reply.getId());
						break;
					}
				}
				//如果分类名称不存在，添加分类入库
				if (insertVo.getParentId() == null) {
					ChatReplyInsertVo insertParentVo = new ChatReplyInsertVo();
					insertParentVo.setName(parentName);
					insertParentVo.setParentId("0");
					insertParentVo.setWeight(getMaxWeight(tenantId,"0")+1);
					insertParentVo.setType(insertVo.getType());
					ChatReply parent = insertParentVo.convert();
					this.save(parent);
					replyParentList.add(parent);
					insertVo.setParentId(parent.getId());
				}
				insertVo.setWeight(getMaxWeight(tenantId,insertVo.getParentId())+1);
				this.save(insertVo.convert());
			} catch (Exception e) {e.printStackTrace(); throw e;}
		}
		return "1";
	}

}
